<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('course.intensify.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button
              icon="Back"
              @click="router.push({name: 'module_education_course_intensify_list', query: {course_id: dataForm.course_id}})">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('course.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-form-item :label="i18n('course.intensify.picture')" prop="picture">
                    <el-upload
                      class="avatar-uploader"
                      :action="$http.adornUrl('/file/picture')"
                      :show-file-list="false"
                      :headers="upload_headers"
                      :on-success="handlePictureSuccess"
                      :before-upload="beforePictureUpload">
                      <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
                      <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                    </el-upload>
                  </el-form-item>
                </el-col>
                <el-col :span="20">
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('course.intensify.title')" prop="title">
                        <el-input
                          v-model="dataForm.title"
                          :placeholder="i18n('common.please_input') + i18n('course.intensify.title')" clearable></el-input>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('course.intensify.parent_title')" prop="parent_id">
                        <el-select v-model="dataForm.parent_id" :placeholder="$t('common.please_select') + $t('course.intensify.parent_title')">
                          <el-option :value="0" label="顶级标题"/>
                          <el-option
                            v-for="(item, key) in intensifyList"
                            :key="key"
                            :label="item.title"
                            :value="item.id"/>
                        </el-select>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('common.sort')" prop="sort">
                        <el-input-number v-model="dataForm.sort" :min="0">
                        </el-input-number>
                      </el-form-item>
                    </el-col>
                  </el-row>
                  <el-row :gutter="20">
                    <el-col :span="8">
                      <el-form-item :label="i18n('common.status')" prop="status">
                        <el-switch
                          v-model="dataForm.status"
                          active-value="1"
                          :active-text="i18n('common.enable')"
                          inactive-value="2"
                          :inactive-text="i18n('common.disable')">
                        </el-switch>
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:education:course:handle')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="course-intensify-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('education/course/intensify')

  const intensifyList = ref([])

  const dataForm = reactive({
    id: 0,
    course_id: 0,
    parent_id: '',
    title: '',
    picture: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('course.intensify.rules.title.require'),
      trigger: 'blur',
    }]
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({
            'course_id': dataForm.course_id
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.parent_id = data.data.parent_id
            dataForm.course_id = data.data.course_id
            dataForm.title = data.data.title
            dataForm.picture = data.data.picture
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate((valid) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${model.value}/handle`),
          method: 'post',
          data: $http.adornData({
            id: dataForm.id || undefined,
            parent_id: dataForm.parent_id,
            course_id: dataForm.course_id,
            title: dataForm.title,
            picture: dataForm.picture,
            sort: dataForm.sort,
            status: dataForm.status,
          }),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.push({name: 'module_education_course_intensify_list', query: {course_id: dataForm.course_id}});
          } else {
            $message.error(i18n(data.message))
          }
        })
      }
    })
  }

  const loadintensifyList = () => {
    $http({
      url: $http.adornUrl('/education/course/intensify/select'),
      method: 'get',
      params: $http.adornParams({
        'course_id': dataForm.course_id
      })
    }).then(({ data }) => {
      if (data && data.status === 200) {
        intensifyList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    dataForm.course_id = query.course_id

    init(formRef)

    loadintensifyList()
  })
</script>

<style lang="scss" scoped>
  .admin_form_main .el-form-item{
    .el-input {
      width: 400px;
    }
    .el-select {
      width: 400px;
    }
    .el-input-number {
      width: 400px;
    }
  }
</style>
